package com.liuya.session.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.liuya.base.AbstractBaseService;
import com.liuya.common.support.Assert;
import com.liuya.session.dao.SysSessionMapper;
import com.liuya.session.model.SysSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;

/**
 * 会话管理
 *
 * @author liuya
 */
@Service("sessionService")
//@CacheConfig(cacheNames = "sysSession")
public class SysSessionService extends AbstractBaseService<SysSession>{
    @Autowired
    private RedisOperationsSessionRepository sessionRepository;
    @Autowired
    private SysSessionMapper sysSessionMapper;

//    @Autowired
//    private SysSessionProvider sysSessionProvider;
//    @DubboReference
//    public void setProvider(SysSessionProvider provider) {
//        this.provider = provider;
//    }

    /**
     * 删除会话
     */
    public void deleteByAccount(String account) {
        Assert.isNotBlank(account, "ACCOUNT");
        List<String> sessionIds = sysSessionMapper.querySessionIdByAccount(account);
        if (sessionIds != null) {
            for (String sessionId : sessionIds) {
                sessionRepository.delete(sessionId);
                sessionRepository.cleanupExpiredSessions();
                sysSessionMapper.deleteBySessionId(sessionId);
            }
        }
    }

    /**
     * 删除会话
     */
//    @CacheEvict
    public void deleteById(String id) {
        Assert.isNotBlank(id, "ID");
        SysSession sysSession = (SysSession) selectById(id);
        if (sysSession != null) {
            sessionRepository.delete(sysSession.getSessionId());
            sessionRepository.cleanupExpiredSessions();
            sysSessionMapper.deleteBySessionId(id);
        }
    }

    /**
     * 更新会话
     */
//    @CachePut
//    @Transactional
    public SysSession update(SysSession record) {
        if (record.getId() == null) {
            record.setUpdateBy(record.getLoginName());
            record.setUpdateTime(new Date());
            String id = sysSessionMapper.queryBySessionId(record.getSessionId());
            if (StringUtils.isNotBlank(id)) {
                record.setId(id);
                sysSessionMapper.updateByPrimaryKey(record);
            } else {
                record.setId(createId("SysSession"));
                record.setCreateBy(record.getLoginName());
                record.setCreateTime(new Date());
                sysSessionMapper.insert(record);
            }
        } else {
            sysSessionMapper.updateByPrimaryKey(record);
        }
        return record;
    }

    /**
     * 删除会话
     */
    public void deleteBySessionId(String sessionId) {
        sysSessionMapper.deleteBySessionId(sessionId);
    }

    public List<String> querySessionIdByAccount(String account) {
        return sysSessionMapper.querySessionIdByAccount(account);
    }

}
